$(function(){
	var admin_id = getCookie('admin_id');
	if(!admin_id){
		window.location.replace('login.html');
	}
	var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem, {
        color: '#1AB394'
    });
    //业务开始
    var id = getQueryString('id');
    if(!id){
    	swal("操作错误", "参数传递错误！", "error");
    	setTimeout(function(){
             location.href="goods_list.html";
		},3000);
		return false;
    }
    //获取基本信息
    $.ajax({
		url: ApiUrl + "/setting/"+id,
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
        	if(result.code == 200){
        		document.getElementById("address").value = result.data.config.address;
        		document.getElementById("phone").value = result.data.config.phone;
        		document.getElementById("shopkeeper").value = result.data.config.shopkeeper;
        		document.getElementById("storeName").value = result.data.config.storeName;
        		document.getElementById("tel").value = result.data.config.tel;
        		document.getElementById("name").value = result.data.name;
        	}else{
        		parent.layer.msg(result.message);
        		return false;
        	}
        }
   });
//提交表单
   $("#submit").click(function(){
   		 if($("#signupForm").valid()){
          var query = $("#signupForm").serialize();
          ajax_post(ApiUrl + "/setting/"+id,"config.html",query);
    	}
   });
   var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            storeName: {
                required: true
            },
            shopkeeper: {
                required: true
            },
            phone: {
                required: true,
                number : true,
                rangelength:[11,11]
            },
            tel: {
                required: true,
                number : true,
            },
            address: {
                required: true
            }
        },
        messages: {
            storeName: {
                required: icon+ "请输入门店名称！"
            },
            shopkeeper: {
                required: icon+ "请输入门店店长！"
            },
            phone: {
                required: icon+ "请输入手机号！",
                number :  icon+"手机号只能是数字" ,
                rangelength: icon+"请输入正确的手机号"
            },
            tel: {
                required: icon+ "请输入座机号！",
                number :  icon+"座机号只能是数字" 
            },
            address: {
                required: icon+ "请输入门店地址！"
            }
        }
    });
});